package com.hdax.dm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hdax.dm.base.DmArea;
import com.hdax.dm.dto.IndexNavDto;
import com.hdax.dm.feign.ImageFeign;
import com.hdax.dm.item.ItemType;
import com.hdax.dm.mappers.ItemTypeMapper;
import com.hdax.dm.mapstructs.IndexNavDtoMapper;
import com.hdax.dm.service.ItemService;
import com.hdax.dm.service.ItemTypeService;
import com.hdax.dm.utils.response.CommonResponse;
import com.hdax.dm.utils.response.ResponseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class ItemTypeServiceImpl extends ServiceImpl<ItemTypeMapper, ItemType> implements ItemTypeService {

    @Autowired
    private IndexNavDtoMapper dtoMapper;
    @Autowired
    private ItemService itemService;

    @Resource
    private RedisTemplate redisTemplate;
    @Autowired
    private ImageFeign imageFeign;
    @Override
    public CommonResponse<List<IndexNavDto>> navs() {
        List<IndexNavDto> dtos = new ArrayList<>();
        if (redisTemplate.opsForValue().get("navs")!=null){
            dtos = (List<IndexNavDto>) redisTemplate.opsForValue().get("navs");
        }else {
            QueryWrapper<ItemType> wrapper = new QueryWrapper<>();
            wrapper.eq("parent",0L);
            List<ItemType> itemTypes = baseMapper.selectList(wrapper);
            dtos = dtoMapper.toNavTarget(itemTypes);
            for (IndexNavDto dto :dtos){
                QueryWrapper<ItemType> wrapper2 = new QueryWrapper<>();
                wrapper2.eq("parent",dto.getId());
                dto.setChildren(dtoMapper.toNavTarget(baseMapper.selectList(wrapper2)));
                dto.setHotItems(itemService.hotItems(dto.getId(),1L));
            }
            redisTemplate.opsForValue().set("navs",dtos);
        }
        return ResponseUtils.returnSuccess(dtos);
    }
    //首页导航数据
    @Override
    public List<ItemType> linenavs(Long parent) {
        QueryWrapper<ItemType> wrapper = new QueryWrapper<>();
        wrapper.eq("parent",parent);
        List<ItemType> itemTypes = null;
        if (redisTemplate.opsForValue().get("linenavs")!=null){
            itemTypes = (List<ItemType>) redisTemplate.opsForValue().get("linenavs");
        }else {
            itemTypes = baseMapper.selectList(wrapper);
            redisTemplate.opsForValue().set("linenavs",itemTypes);
        }
        return itemTypes;
    }
    //搜索列表页面展示内容
    @Override
    public CommonResponse<List<ItemType>> linenavs(String parent, String param) {
        QueryWrapper<ItemType> wrapper = new QueryWrapper<>();
        if(parent!=null && !parent.equals("")){
            wrapper.eq("parent",parent);
        }else {
            wrapper.eq("parent",param);
        }
        List<ItemType> itemTypes = baseMapper.selectList(wrapper);
        return ResponseUtils.returnSuccess(itemTypes);
    }
    //搜索列表页面展示城市
    @Override
    public CommonResponse<List<DmArea>> area() {
        return  ResponseUtils.returnSuccess(imageFeign.dmArea());
    }
}
